
package com.deco.back.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 时间共计
 *
 * @author hzy
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class DateUtil {
    
    public static final String DEFAULT_DATE_FORMAT_PATTERN_SHORT = "yyyy-MM-dd";

    public static final String DEFAULT_DATE_FORMAT_PATTERN_FULL = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT_PATTERN_YYYY = "yyyy";
    
    private static Map<String, SimpleDateFormat> dateFormatCache = new ConcurrentHashMap<String, SimpleDateFormat>();

    /**
     * 获取当前日期前（-n）或后（n）天
     *
     * @param d
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("deprecation")
    public static Timestamp getTimeByDay(int d, Timestamp dtm) {
        Timestamp tmp = new Timestamp(dtm.getTime());
        tmp.setDate(tmp.getDate() + d);
        return tmp;
    }

    /**
     * 获取当前日期前（-n）或后（n）月
     *
     * @param n
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("deprecation")
    public static Timestamp getTimeByMonth(int n, Timestamp dtm) {
        Timestamp tmp = new Timestamp(dtm.getTime());
        tmp.setMonth(tmp.getMonth() + n);
        return tmp;
    }

    /** fmt yyyyMM */
    public static final SimpleDateFormat monthFmt1 = new SimpleDateFormat("yyyyMM");

    public static String getCurrentMonth() {
        Timestamp dtm = new Timestamp(System.currentTimeMillis());
        return monthFmt1.format(dtm);
    }
    
    /**
     * 以yyyy-MM-dd HH:mm:ss形式返回当前时间的字符串
     *
     * @return
     */
    public static String getFullCurrentDateStr() {
        return getCurrentDateString(DEFAULT_DATE_FORMAT_PATTERN_FULL);
    }

    /**
     * 以yyyy-MM-dd形式返回当前日期的字符串
     *
     * @return
     */
    public static String getShortCurrentDateStr() {
        return getCurrentDateString(DEFAULT_DATE_FORMAT_PATTERN_SHORT);
    }

    /**
     * 返回pattern所指定的当前时间的字符串
     *
     * @param pattern
     * @return
     */
    public static String getCurrentDateString(String pattern) {
        if (pattern == null || "".equals(pattern.trim())) {
            return null;
        }
        SimpleDateFormat sdf = null;
        if (dateFormatCache.containsKey(pattern)) {
            sdf = dateFormatCache.get(pattern);
        } else {
            try {
                sdf = new SimpleDateFormat(pattern);
                dateFormatCache.put(pattern, sdf);
            } catch (Exception e) {
                e.printStackTrace();
                sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_PATTERN_FULL);
            }
        }
        return sdf.format(new Date());
    }

    /**
     * 返回时间date所指定的日期格式的字符串形式
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateString(Date date, String pattern) {
        if (date == null || pattern == null || "".equals(pattern.trim())) {
            return null;
        }
        SimpleDateFormat sdf = null;
        if (dateFormatCache.containsKey(pattern)) {
            sdf = dateFormatCache.get(pattern);
        } else {
            try {
                sdf = new SimpleDateFormat(pattern);
                dateFormatCache.put(pattern, sdf);
            } catch (Exception e) {
                e.printStackTrace();
                sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_PATTERN_FULL);
            }
        }
        return sdf.format(date);
    }

    /**
     * 把Date按照yyyy-MM-dd格式，返回
     *
     * @param date
     * @return
     */
    public static String getShortDateStr(Date date) {
        return getDateString(date, DEFAULT_DATE_FORMAT_PATTERN_SHORT);
    }

    public static String getFullDateStr(Date date) {
        return getDateString(date, DEFAULT_DATE_FORMAT_PATTERN_FULL);
    }
    
}
